package tools

import (
	"encoding/json"

	"github.com/astaxie/beego"
	//redis缓存引擎
	_ "github.com/astaxie/beego/cache/redis"
	//引入缓存模块
	"github.com/astaxie/beego/cache"
)

// GetRedis 获取redis连接实例
func GetRedis() (adapter cache.Cache, err error) {

	redisKey := beego.AppConfig.String("rediskey")
	redisAddr := beego.AppConfig.String("redisaddr")
	redisPort := beego.AppConfig.String("redisport")
	redisdbNum := beego.AppConfig.String("redisdbnum")

	redisConfigMap := map[string]string{
		"key":   redisKey,
		"conn":  redisAddr + ":" + redisPort,
		"dbNum": redisdbNum,
	}
	redisConfig, _ := json.Marshal(redisConfigMap)

	cacheConn, err := cache.NewCache("redis", string(redisConfig))
	if err != nil {
		return nil, err
	}
	return cacheConn, nil
}
